package cn.doitedu.ml.examples;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Title:
 * @Package
 * @Description:
 * @Author hunter@doitedu.cn
 * @date 2020/8/1614:43
 */
public class Image2VectorUtil {
    public static void main(String[] args) throws IOException {

        File imgDir = new File("C:\\Users\\coder\\Desktop\\digits");
        File[] files = imgDir.listFiles();

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\coder\\Desktop\\digits.txt"));

        for (File file : files) {
            BufferedImage bi = ImageIO.read(file);
            String label = "";
            if(file.getName().contains("3")){
                label = "3";
            }else if(file.getName().contains("8")){
                label = "8";
            }
            bw.write(label+",");
            for(int i=0;i<32;i++){
                for(int j=0;j<32;j++){
                    int rgb = bi.getRGB(j, i);
                    bw.write(rgb==-1?"0":"1");
                }
            }
            bw.newLine();
        }

        bw.close();

    }
}